﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Settings;

namespace ProjectBase.Data.Db
{
    /// <summary>
    ///     Base class for ADO.NET operations.
    ///     Instantiates and keeps one IDbCommand instance, 
    ///     which is available during the whole Requeste.
    ///     If controllers's action is decorate with [Transaction] attribute
    ///     (or transaction is called externally e.g. for batch or Unit tests)
    ///     all operation can be simply commited or rolled back.
    /// </summary>
    public abstract class DbDao<TDbCommand> : IDao
        where TDbCommand : class, IDbCommand
    {
        #region protected
        /// <summary>
        ///     Lazily (when firstly needed) instantiated instance of the IDbCommand
        ///     available through the whole request time for all needed Dao operations.
        ///     The underlying type is based on the setting in the 
        ///     <see cref = "IBaseSettingsProvider">IBaseSettingsProvider</see> 
        ///     which is by default implemented on the Data layer.
        ///     Its 'IDbCommand NewDbCommand()' operation serves as the factory for all the application commands
        /// </summary>
        protected virtual TDbCommand DbCommand
        {
            get { return DbSession.Current as TDbCommand; }
        }

        /// <summary>
        ///     Property which is expected to be instantiated via<see cref = "IDbCommand">IDbCommand Command</see>
        ///     in the last step of the Find operation.
        ///     DataReader will in next steps Converted via e.g. CreateEntity(reader) operation into real entity.
        ///     resulted list could be sorted, paged and even filtered (LINQ)
        /// </summary>
        protected virtual DbDataReader DataReader { get; set; }
        #endregion protected
    }
}